package com.xframe.event.bus.core.msg;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xframe.event.bus.core.config.ConnectInfo;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

//@Component
public class ConnectFactory {

    private static Connection msgConn;

    public static Connection getMsgConn(){
        return msgConn;
    }

    public static Connection createConnection(ConnectInfo connectInfo) throws IOException, TimeoutException {
        if(msgConn == null) {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setUsername(connectInfo.getUserName());
            factory.setPassword(connectInfo.getPassword());
            factory.setVirtualHost(connectInfo.getVirtualHost());
            factory.setHost(connectInfo.getHost());
            factory.setPort(connectInfo.getPort());
            msgConn = factory.newConnection();
        }
        return msgConn;
    }
}
